package com.demo.eshop.pdetail.config;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author: E20160504
 * @date: 2018/10/24
 * @desc
 */

@Configuration
public class RedisConfig {

    @Autowired
    private RedisProperties redisProperties;

    @Bean
    public JedisCluster JedisCluster() {

        String[] masters = redisProperties.getNodes().split(",");
        Set<HostAndPort> nodes = new HashSet<>();

        for (String ipPort : masters) {
            nodes.add(HostAndPort.parseString(ipPort));
        }

        if(redisProperties.getPassword() != null && !redisProperties.getPassword().isEmpty()){
            return new JedisCluster(nodes,redisProperties.getConnectionTimeout(),redisProperties.getSoTimeout(),redisProperties.getMaxAttempts(),
                    redisProperties.getPassword(),new GenericObjectPoolConfig());
        }
        return new JedisCluster(nodes);
    }
}
